package com.example.stringbufferlogger.controller;

import com.example.stringbufferlogger.Utils.ExcelUtils;
import com.example.stringbufferlogger.Utils.OSSUtil;
import com.example.stringbufferlogger.entity.*;
import com.example.stringbufferlogger.mapper.FileConfigMapper;
import com.example.stringbufferlogger.service.FileCompareRecordService;
import com.example.stringbufferlogger.service.FileConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/fileCompare")
public class FileCompareController {

    @Autowired
    private FileCompareRecordService fileCompareRecordService;
    @Autowired
    private FileConfigService fileConfigService;
    @Autowired
    private ExcelUtils excelUtils;
    @Autowired
    private FileConfigMapper fileConfigMapper;

    // 原有的添加 FileConfig 的方法
    @PostMapping("/add")
    public boolean addFileConfig(@RequestBody FileConfig fileConfig) {
        return fileConfigService.save(fileConfig);
    }

    /**
     * 查询所有文件配置
     *
     * @return 文件配置列表
     */
    @GetMapping("/fileConfig/all")
    public List<FileConfig> getAllFileConfigs() {
        return fileConfigService.getAllFileConfigs();
    }


    @PostMapping("/Oss")
    public ResponseEntity<Map<String, Object>> fileTest(String requestId) {
        Map<String, Object> response = new HashMap<>();
        try {
            String tempDir = System.getProperty("java.io.tmpdir");
            String fileName =  requestId+fileConfigMapper.selectFileTypeByRequestId(requestId);
            OSSUtil.getFileFromOSS(tempDir,fileName,"old");
            OSSUtil.getFileFromOSS(tempDir,fileName,"new");
            excelUtils.processOssExcel(requestId);
            response.put("status", "success");
            response.put("message", "Log and files processed successfully");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "Error processing log: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }

    }

    /**
     * 根据 interface_url 和 request_id 对比记录，若参数均为空则使用 ReplayLog 数据对比
     *
     * @param interfaceUrl FILE_DATA_RECORD 的 interface_url，对应 FILE_CONFIG 的 interface_adrs
     * @param requestId    请求 ID
     * @return 文件对比记录列表
     * @throws IllegalArgumentException 未找到可参与比对的文件配置时抛出异常
     */
    @PostMapping("/compare")
    public List<FileCompareRecord> compareRecords(
            @RequestParam(required = false) String interfaceUrl,
            @RequestParam(required = false) String requestId) {


        return fileCompareRecordService.compareByReplayLogData();
//        if (interfaceUrl == null && requestId == null) {
//            return fileCompareRecordService.compareByReplayLogData();
//        } else if (requestId != null) {
//            return fileCompareRecordService.compareByRequestId(requestId);
//        } else if (interfaceUrl != null) {
//            return fileCompareRecordService.compareByInterfaceUrl(interfaceUrl);
//        } else {
//            return fileCompareRecordService.compareByInterfaceAndRequest(interfaceUrl, requestId);
//        }
    }
}